package com.sweet.satellite;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.bilibili.magicasakura.utils.ThemeUtils;
import com.bilibili.magicasakura.widgets.TintToolbar;
import com.sweets.look.R;
import com.sweets.look.preference.PreferenceManager;
import com.sweets.look.util.ViewHelper;
import com.sweets.look.util.ViewUtils;


public abstract class TranslucentSBActivity extends AppCompatActivity {

    protected boolean needSetTranslucent = true, isCreated = false;
    protected Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        createTranslucentStatusBar();
        //设置状态栏颜色
        PreferenceManager pm = PreferenceManager.getInstance();
        if (!pm.getHideStatusBarEnabled()) {//全屏打开
            ViewHelper.setStatusBarTintColor(this, ThemeUtils.getColorById(this, R.color.theme_color_primary));
        }
        ViewHelper.switchFullScreenMode(this);
    }

    protected void setToolbarTitle(@StringRes int resId) {
        try {
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            toolbar.setTitle(resId);
            setSupportActionBar(toolbar);
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowTitleEnabled(true);
        } catch (NullPointerException e) {
            //do nothing
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    protected void createTranslucentStatusBar() {
        if (needSetTranslucent) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                setTranslucentNavigation();
            }
        }
        isCreated = true;
    }


    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    protected void setTranslucentNavigation() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

        }
    }

    public void setStatusBarTranslucent(boolean should) throws ShouldSetBeforeActivityCreatedException {
        if (isCreated) {
            throw new ShouldSetBeforeActivityCreatedException();
        } else {
            this.needSetTranslucent = should;
        }
    }

    public class ShouldSetBeforeActivityCreatedException extends Exception {
    }

}
